﻿using JSM.DataStore;

namespace JSM
{
	/// <summary>
	/// Extension methods for the use of a JavaScript Model.
	/// </summary>
	public static class IJavaScriptModelAwareExtensions
	{
		/// <summary>
		/// Adds a javascript variable.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="variableName">The variable.</param>
		/// <param name="value">The value.</param>
		/// <param name="pageLoadEvent">The page load event.</param>
		/// <param name="dataStoreProvider">
		/// An optional data store provider.
		/// </param>
		public static void AddJavaScriptVariable(this IJavaScriptModelAware javaScriptModelAware, string variableName, object value, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, IDataStoreProvider dataStoreProvider = null)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).AddVariable(variableName, value, pageLoadEvent);
		}

		/// <summary>
		/// Adds multiple javascript variables.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="variables">Multiple javascript variables beeing passed as: string, dynamic, string, dynamic ...</param>
		/// <param name="pageLoadEvent">The <see cref="PageLoadEvent"/>.</param>
		/// <param name="dataStoreProvider">
		/// An optional data store provider.
		/// </param>
		public static void AddJavaScriptVariables(this IJavaScriptModelAware javaScriptModelAware, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, IDataStoreProvider dataStoreProvider = null, params object[] variables)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).AddVariables(pageLoadEvent, variables);
		}

		/// <summary>
		/// Adds multiple javascript variables.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="variables">Multiple javascript variables beeing passed as: string, dynamic, string, dynamic ...</param>
		/// <param name="pageLoadEvent">The <see cref="PageLoadEvent"/>.</param>
		public static void AddJavaScriptVariables(this IJavaScriptModelAware javaScriptModelAware, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, params object[] variables)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware).AddVariables(pageLoadEvent, variables);
		}

		/// <summary>
		/// Adds multiple javascript variables.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="variables">Multiple javascript variables beeing passed as: string, dynamic, string, dynamic ...</param>
		public static void AddJavaScriptVariables(this IJavaScriptModelAware javaScriptModelAware, params object[] variables)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware).AddVariables(PageLoadEvent.Ready, variables);
		}

		/// <summary>
		/// Adds a javascript function to the execute queue.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="function">The function.</param>
		/// <param name="pageLoadEvent">The page load event when the function should be executed.</param>
		/// <param name="dataStoreProvider">
		/// An optional data store provider.
		/// </param>
		/// <param name="functionParameters">Optional parameters to pass to the javascript function.</param>
		public static void AddJavaScriptFunction(this IJavaScriptModelAware javaScriptModelAware, string function, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, IDataStoreProvider dataStoreProvider = null, params object[] functionParameters)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).AddFunction(function, pageLoadEvent, functionParameters);
		}

		/// <summary>
		/// Adds a javascript function to the execute queue.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="function">The function.</param>
		/// <param name="pageLoadEvent">The page load event when the function should be executed.</param>
		/// <param name="functionParameter">Optional parameters to pass to the javascript function.</param>
		public static void AddJavaScriptFunction(this IJavaScriptModelAware javaScriptModelAware, string function, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, params object[] functionParameter)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware).AddFunction(function, pageLoadEvent, functionParameter);
		}

		/// <summary>
		/// Adds a javascript function to the execute queue.
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="function">The function.</param>
		/// <param name="functionParameter">Optional parameters to pass to the javascript function.</param>
		public static void AddJavaScriptFunction(this IJavaScriptModelAware javaScriptModelAware, string function, params object[] functionParameter)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware).AddFunction(function, PageLoadEvent.Ready, functionParameter);
		}

		/// <summary>
		/// Inserts the function, to set the function to the first of list
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="function">The function.</param>
		/// <param name="pageLoadEvent">The load event.</param>
		/// <param name="dataStoreProvider">
		/// An optional data store provider.
		/// </param>
		/// <param name="functionParameters">Optional parameters to pass to the javascript function.</param>
		public static void InsertJavaScriptFunction(this IJavaScriptModelAware javaScriptModelAware, string function, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, IDataStoreProvider dataStoreProvider = null, params object[] functionParameters)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).InsertFunction(function, pageLoadEvent, functionParameters);
		}

		/// <summary>
		/// Inserts the function, to set the function to the first of list
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="function">The function.</param>
		/// <param name="pageLoadEvent">The load event.</param>
		/// <param name="functionParameters">Optional parameters to pass to the javascript function.</param>
		public static void InsertJavaScriptFunction(this IJavaScriptModelAware javaScriptModelAware, string function, PageLoadEvent pageLoadEvent = PageLoadEvent.Ready, params object[] functionParameters)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware).InsertFunction(function, pageLoadEvent, functionParameters);
		}

		/// <summary>
		/// Inserts the function, to set the function to the first of list
		/// </summary>
		/// <param name="javaScriptModelAware">An object implementing the <see cref="IJavaScriptModelAware"/> interface.</param>
		/// <param name="function">The function.</param>
		/// <param name="functionParameters">Optional parameters to pass to the javascript function.</param>
		public static void InsertJavaScriptFunction(this IJavaScriptModelAware javaScriptModelAware, string function, params object[] functionParameters)
		{
			JavaScriptModel.GetJavaScriptModel(javaScriptModelAware).InsertFunction(function, PageLoadEvent.Ready, functionParameters);
		}
	}
}
